/*
  (C) 2007 - Boston University
  Contact: Michael Ocean 

	This class is taken directly from a CS511 submission
	
  This work is a part of the larger snBench project.  Information
  can be found at: http://csr.bu.edu/snbench 

  This program is free software; you can redistribute it and/or
  modify it under the terms of Version 2 of the GNU General Public 
  License.
  
  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  General Public License for more details.
  
  You should have received a copy of the GNU Library General Public
  License along with this library; if not, write to the Free
  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
package stepide.xml;

import java.io.FileOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.io.StringReader;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.InputSource; 

import stepide.ErrorManager;


/*
 * Allows easy loading, saving, and viewing raw xml of XML documents. Analogous to .Net XmlDocument class
 */
public class XmlDocument extends XmlNode 
{
	private Document _document = null;
	private String _rawXml;

	/**
		Requires: The input file is a valid XML file
		Effects: Loads this XmlDocument with the XML contents of the specified file
	*/
	public void load(File file) 
	{
		try {
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			factory.setValidating(true);
			DocumentBuilder builder = factory.newDocumentBuilder();
			builder.setErrorHandler(null);
			_document = builder.parse(file.getPath());
			_node = getDocumentElement();
		}
		catch (javax.xml.parsers.ParserConfigurationException ex) {
			ErrorManager.LogError(ex);
		}
		catch (org.xml.sax.SAXException ex) {
			ErrorManager.LogError(ex);
		}
		catch (java.io.IOException ex) {
			ErrorManager.LogError(ex);
		}	
		
	}

	/**
		Requres: The string is a valid XML document
		Effects: Loads this XmlDocument with the xml contents of the specified string
	*/
	public void loadXml(String xml)
	{
		_rawXml = xml;
		try
		{
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		factory.setValidating(true);
		DocumentBuilder builder = factory.newDocumentBuilder();
		builder.setErrorHandler(null);
		StringReader reader = new StringReader(xml);
		InputSource is = new InputSource(reader);	
		_document = builder.parse(is);
		_node = getDocumentElement();
		}
		catch(javax.xml.parsers.ParserConfigurationException ex)
		{
			ErrorManager.LogError(ex);
		}
		catch(org.xml.sax.SAXException ex)
		{
			ErrorManager.LogError(ex);
		}
		catch(java.io.IOException ex)
		{
			ErrorManager.LogError(ex);
		}
	}

	/**
		Effects: Returns the raw XML of this XmlDocument
	*/
	public String toString()
	{
		if(_rawXml == null)
		{
					
		OutputStream outStream = null;
		try 
		{
			outStream = new ByteArrayOutputStream();
			TransformerFactory transformerFactory = TransformerFactory.newInstance();
			Transformer transformer = transformerFactory.newTransformer();
			DOMSource source = new DOMSource(_document);
			StreamResult result = new StreamResult(outStream);
			transformer.transform(source, result);

		}
		catch (javax.xml.transform.TransformerConfigurationException ex)
		{
			ErrorManager.LogError(ex);
		}
		catch (javax.xml.transform.TransformerException ex) 
		{
			ErrorManager.LogError(ex);
		}
		finally 
		{
			try 
			{
				if (outStream != null) 
				{
					outStream.flush();
					_rawXml = outStream.toString();
				}
			}
			catch (java.io.IOException ex) 
			{
				ErrorManager.LogError(ex);
			}
		}
		}
		return _rawXml;
	}

	/**
		Effects: Saves this XML document to the specified file path
	*/
	public void save(String filePath)
	{
		OutputStream outStream = null;

		try {
			outStream = new FileOutputStream(filePath);
			TransformerFactory transformerFactory = TransformerFactory.newInstance();
			Transformer transformer = transformerFactory.newTransformer();
			DOMSource source = new DOMSource(_document);
			StreamResult result = new StreamResult(outStream);
			transformer.transform(source, result);

		}
		catch (java.io.IOException ex) {
			ErrorManager.LogError(ex);
		}
		catch (javax.xml.transform.TransformerConfigurationException ex) {
			ErrorManager.LogError(ex);
		}
		catch (javax.xml.transform.TransformerException ex) {
			ErrorManager.LogError(ex);
		}
		finally {
			try {
				if (outStream != null) {
					outStream.flush();
					outStream.close();
				}
			}
			catch (java.io.IOException ex) {
				ErrorManager.LogError(ex);
			}
		}
	}

	/**
		Effects: Returns the Document Element (root element) of this Xml Document
	*/
	public Node getDocumentElement()
	{
		return _document.getDocumentElement();
	}


	/**
		Effects: Returns the org.w3c.Document representing this XmlDocument
	*/		
	public Document getDocument()
	{
		return _document;
	}
}